﻿using Management.Auxiliary;
using Management.Core.Workshop;
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Linq;

namespace Management.Areas.Control.Controllers
{
    [AdminAuthorize]
    public class WorkshopController : Controller
    {
        private WorkshopManager wm = new WorkshopManager();
        //
        // GET: /Control/Workshop/
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 车间列表
        /// </summary>
        /// <param name="workshopId"></param>
        /// <param name="name"></param>
        /// <param name="pageNumber"></param>
        /// <param name="pageSize"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        public ActionResult PageList(int? workshopId, string name, int? pageNumber, int? pageSize, int? order)
        {
            Paging<Workshop> _pagingWorkshop = new Paging<Workshop>();
            if (pageNumber != null && pageNumber > 0) _pagingWorkshop.PageIndex = (int)pageNumber + 1;
            if (pageSize != null && pageSize > 0) _pagingWorkshop.PageSize = (int)pageSize;
            var _paging = wm.FindPageList(_pagingWorkshop, workshopId, name, null);
            return Json(new { total = _paging.TotalNumber, rows = _paging.Items });
        }
        
        /// <summary>
        /// 增加
        /// </summary>
        /// <returns></returns>
        public ActionResult Add()
        {
            var _manufacturings = new Management.Core.Manufacturing.ManufacturingManager().FindList();
            List<SelectListItem> _listItems = new List<SelectListItem>(_manufacturings.Count());
            foreach (var manufacturing in _manufacturings)
            {
                _listItems.Add(new SelectListItem() { Text = manufacturing.Name, Value = manufacturing.ManufacturingID.ToString() });
            }
            ViewBag.Manufacturings = _listItems;
            List<SelectListItem> _listItems2 = new List<SelectListItem>(2);
            _listItems2.Add(new SelectListItem() { Text = "开启", Value = "1" });
            _listItems2.Add(new SelectListItem() { Text = "禁止", Value = "0" });
            ViewBag.IsDisabled = _listItems2;
            return PartialView();
        }

        /// <summary>
        /// 增加车间
        /// </summary>
        /// <param name="_workshop"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Add([Bind(Include = "WorkshopID, Name, ManufacturingID, SubID, IsDisabled, Created_at, Updated_at")]Workshop _workshop)
        {
            _workshop.Created_at = DateTime.Now;
            _workshop.Updated_at = DateTime.Now;
            Response _resp = new Response() { Code = -1 };
            if (ModelState.IsValid)
            {
                //检查名称
                if (wm.IsContains(u => u.Name == _workshop.Name))
                {
                    _resp.Code = 0;
                    _resp.Message = "车间存在";
                }
                else
                {
                    _resp = wm.Add(_workshop);
                }
            }
            else
            {
                _resp.Code = 0;
                _resp.Message = General.GetModelErrorString(ModelState);
            }
            return Json(_resp);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="workshopid"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Delete(int workshopid)
        {
            Response _resp = new Response();
            var _workshop = wm.Find(workshopid);
            if (_workshop == null)
            {
                _resp.Code = 0;
                _resp.Message = "要删除条目不存在";
            }
            else
            {
                _resp = wm.Delete(_workshop);
            }
            return Json(_resp);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Modify(int id)
        {
            var _manufacturings = new Management.Core.Manufacturing.ManufacturingManager().FindList();
            List<SelectListItem> _listItems = new List<SelectListItem>(_manufacturings.Count());
            foreach (var manufacturing in _manufacturings)
            {
                _listItems.Add(new SelectListItem() { Text = manufacturing.Name, Value = manufacturing.ManufacturingID.ToString() });
            }
            ViewBag.Manufacturings = _listItems;
            List<SelectListItem> _listItems2 = new List<SelectListItem>(2);
            _listItems2.Add(new SelectListItem() { Text = "开启", Value = "1" });
            _listItems2.Add(new SelectListItem() { Text = "禁止", Value = "0" });
            ViewBag.IsDisabled = _listItems2;
            return PartialView(wm.Find(id));
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Modify(int id, FormCollection form)
        {
            Response _resp = new Response();
            var _workshop = wm.Find(id);
            _workshop.Updated_at = DateTime.Now;
            if (TryUpdateModel(_workshop, new string[] { "Name", "ManufacturingID", "SubID", "IsDisabled", "Created_at", "Updated_at" }))
            {
                if (wm.IsContains(u => u.Name == _workshop.Name) && form["Name"] != _workshop.Name)
                {
                    _resp.Code = 0;
                    _resp.Message = "名称已存在";
                }
                else
                {
                    _resp = wm.Update(_workshop);
                }
            }
            else
            {
                _resp.Code = 0;
                _resp.Message = "模型不符合条件，更新失败";
            }
            return Json(_resp);
        }
	}
}